# @Time:2023/4/25 15:52
# -*- coding: UTF-8 -*-
from django.contrib.auth.backends import ModelBackend
from django.db.models import Q
from rest_framework import serializers
from user.models import User
#自定义用户登录认证类，提供多认证方式
class MyBackend(ModelBackend):
    def authenticate(self, request,username=None,password=None,**kwargs):
        try:
            #查找用户信息
            user=User.objects.get(Q(username=username) | Q(mobile=username) | (Q(email=username)))
        except:
            raise serializers.ValidationError({'error':"未找到该用户"})
        else:
            #验证密码
            if user.check_password(password):
                return user
            else:
                raise serializers.ValidationError({'error':"密码错误"})

